package org.reliablesource.prism.core.internal;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import org.reliablesource.prism.core.IPrismAnnotation;
import org.reliablesource.prism.core.IPrismScreen;
import org.reliablesource.prism.core.IPrismTutorial;

/**
 * 
 * Created on: Feb 25, 2006<br>
 * 
 * @author Dennis Park <a
 *         href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 * 
 */

class PrismScreen extends PrismUnit implements IPrismScreen {

	private static final long serialVersionUID = 8714292175665379730L;
	// Mouse screen coordinate position
	private Point mouseLocation;

	// File info
	private String path;

	private String type;

	private final List<IPrismAnnotation> annotationList;

	private int eventType;
	private String eventWidget;
	private long eventDate;
	private int widgetId;
	private Object widget;

	// Storage for a user defined coordinate list for a mouse pointer path
	private final List<Point> pathCoordinateList;

	private static final String DEFAULT_PATH = "D:/temp/";

	PrismScreen() {
		super();
		annotationList = new ArrayList<IPrismAnnotation>();
		pathCoordinateList = new ArrayList<Point>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#setImageFileType(java.lang.String)
	 */
	public void setImageFileType(String _type) {
		type = _type;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#setImageFilePath(java.lang.String)
	 */
	public void setImageFilePath(String _path) {
		path = _path;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#getImageFileType()
	 */
	public String getImageFileType() {
		return type;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#getImageFilePath()
	 */
	public String getImageFilePath() {
		if (path == null)
			return DEFAULT_PATH;
		return path;
	}

	public IPrismAnnotation getAnnotation(int i) {
		if (i >= 0 && i <= annotationList.size() - 1)
			return annotationList.get(i);
		return null;
	}

	public synchronized boolean addAnnotation(IPrismAnnotation _annotation) {
		if (!annotationList.contains(_annotation))
			return annotationList.add(_annotation);
		return false;
	}

	public synchronized boolean removeAnnotation(IPrismAnnotation _annotation) {
		return annotationList.remove(_annotation);
	}

	public synchronized boolean addPathLocation(int _idx, Point _pt) {

		if (null != mouseLocation)
			if (mouseLocation.equals(_pt))
				return false;

		if (pathCoordinateList.contains(_pt))
			return false;

		if (pathCoordinateList.size() + 1 == _idx)
			pathCoordinateList.add(_pt);
		else
			pathCoordinateList.add(_idx, _pt);
		return true;

	}

	public Point getPathLocation(int i) {
		if (i >= 0 && i <= pathCoordinateList.size() - 1)
			return pathCoordinateList.get(i);
		return null;
	}

	public synchronized boolean removePathLocation(int _idx, Point _p) {
		if (null != mouseLocation)
			if (mouseLocation.equals(_p))
				return false;

		return pathCoordinateList.remove(_p);
	}

	public IPrismAnnotation[] getAnnotations() {
		return annotationList.toArray(new IPrismAnnotation[annotationList
				.size()]);
	}

	public synchronized List<IPrismAnnotation> getAnnotationsList() {
		return annotationList;
	}

	public Point getPointerStart() {
		return mouseLocation;
	}

	public void setPointerStart(Point _point) {
		mouseLocation = _point;
	}

	public Point[] getPathLocationsPointList() {
		return pathCoordinateList.toArray(new Point[pathCoordinateList.size()]);
	}

	public synchronized List<Point> getPathLocationsList() {
		return pathCoordinateList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#getPointerEnd()
	 * 
	 * The PointerEnd value is the PointerStart value of the next screen.
	 */
	public Point getPointerEnd() {

		int seq = ((IPrismTutorial) getParent()).getChildSequence(this);

		if (seq + 1 < ((IPrismTutorial) getParent()).getChildListSize())
			return ((IPrismScreen) ((IPrismTutorial) getParent())
					.getChild(seq + 1)).getPointerStart();
		else
			return getPointerStart();
	}

	public int getEventType() {
		return eventType;
	}

	public void setEventType(int _eventType) {
		eventType = _eventType;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#getEventDate()
	 */
	public long getEventDate() {
		return eventDate;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#getEventWidget()
	 */
	public String getEventWidget() {
		return eventWidget;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#setEventDate(java.sql.Date)
	 */
	public void setEventDate(long _date) {
		eventDate = _date;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#setEventWidget(java.lang.String)
	 */
	public void setEventWidget(String _widget) {
		eventWidget = _widget;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#getWidgetId()
	 */
	public int getWidgetId() {
		return widgetId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.prism.core.IPrismScreen#setWidgetId(int)
	 */
	public void setWidgetId(int _id) {
		widgetId = _id;
	}

	public Object getWidget() {
		return widget;
	}

	public void setWidget(Object _widget) {
		widget = _widget;
	}
}
